// new testFF().consoleListener();//暂时注释		 
function getUrlKey(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [,
        ""
    ])[1].replace(/\+/g, '%20')) || null;
}
// console.log(getUrlKey('code'));
var code = getUrlKey('code');
var fcode = '';
var ST = eval("'" + "\u004d\u006a\u0041\u0078\u004f\u0053\u0030\u007a\u004c\u0054\u0049\u007a" + "'");

if (code && code.length > 7 && code.length < 12) {
    $(".referralCode").val("您的推荐人是:" + code);
    isSuccess();
} else {
    // $("form").hide();
    $("form,.welcome,.wait").remove();
    setTimeout(function () {					
        layer.alert(
            "\u8bf7\u8054\u7cfb\u60a8\u7684\u597d\u53cb\u786e\u8ba4\u63a8\u8350\u7801\u7684\u6b63\u786e\u6027"
        ); //请联系您的好友确认推荐码的正确性
    });
}

function isSuccess() {
     var nd = mkD();
     var s= window.btoa(nd) ;
       s= solt(s);
     var flag = localStorage.getItem(s);
     console.log(flag);				 
    if(flag=="test"||flag){
        loadingReg = true;
        $(".wait").fadeOut(50);
        $(".success").fadeIn(100);
        $("form,.welcome,.wait").remove();
    }else{
        $("form").show();
    }
}
var ss = "u8bf7-u786e-u8ba4-u53c2-u6570-u6b63-u786e-u6027";
localStorage.setItem(ST, window.btoa(ss));
var fp1 = new testF({
    canvas: true
}); //

function mkD() {
    var date = new Date()
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var nd = y + "-" + m + "-" + d;
    return nd;
}

function solt(data) {//先bs 再加盐
    var addSloat = "Az";
    var op =
        data.substring(0, 3) + addSloat + data.substring(3, data.length);
    // console.log(op)
    return  op;
    
}
fcode = fp1.get();
var loadingReg = false;

$('#regist-button').click(function (event) {
    event.preventDefault();
    console.log(fcode);
    if (!fcode) {
        layer.alert("\u975e\u6cd5\u6ce8\u518c"); //.非法
        window.location.reload();
        return
    }

    var te = localStorage.getItem(ST);
    if (!te) {
        layer.alert("\u53c2\u6570\u9519\u8bef"); //参数错误
        return
    }

    if (loadingReg) {
        return
    }
    var tel = $(".phone").val();
    var psw = $(".psw").val();
    var psw2 = $(".psw2").val();
    var vcode = $(".code").val();
    if (!isOk()) {
        return
    }

    loadingReg = true;
    waitting();

    var param = {
        phone: tel,
        psw: psw,
        vcode: vcode,
        code: code
    }

    ajax("/Api/registe", param, success, fail)

    function isOk() {
        console.log(tel, psw, psw2, vcode)
        if (!tel || !psw || !psw2 || !vcode) {
            layer.msg("请输入要注册的密码帐号和验证码");
            return false
        }
        // var name = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/;
        // if (!name.test(tel)) {
        //     layer.msg("手机号码格式错误");
        //     return false;
        // }
        var password = /^.*(?=.{6,30})(?=.*\d)(?=.*[a-zA-Z]).*$/;

        if (!password.test(psw)) {
            layer.msg("密码必须包含数字和字母,或有特殊字符且长度至少6位");
            return false;
        }

        if (psw !== psw2) {
            layer.msg("二次输入密码不一置");
            return false
        }
        if (vcode.length != 4) {
            layer.msg("验证码错误");
            return false
        }
        return true;
    }

    function success(e) {
        loadingReg = false;
        // console.log("暂时全部不处理",e)
        // return
        if (e.success) {
            $(".wait").fadeOut(50);
            $(".download-BT").attr("href",e.url)
            localStorage.setItem('app', e.url);
            setTimeout(function () {
                $(".success").fadeIn(100);
                // var nd = mkD();
                // nd=	window.btoa(nd);//t先bs 再加壏
                // var newD=  solt(nd);
                // localStorage.setItem(newD, "test");
                // localStorage.setItem(window.btoa(nd), "")
                // console.log(nd);
            }, 45)
        } else {
            fail(e)
        }
    }




    function fail(e) {
        console.log(e)
        // loadingReg = false;
        // console.log("暂时全部不处理")
        // return

        layer.msg(e.msg);
        $(".welcome").fadeOut(110);
        $('.wrapper').removeClass('form-success');
        $('form').fadeIn(500);
        $(".wait").fadeOut();
        $(".welcome").fadeIn(110);
        loadingReg = false;
    }

    function waitting() {
        $(".wait").fadeIn(150);
        $(".welcome").fadeOut(110);
        $('form').fadeOut(500);
        $('.wrapper').addClass('form-success');

    }

});

getVeriCode();
var loadingCode = false;

function getVeriCode() {
    if (loadingCode) {
        return
    }
    loadingCode = true;
    var data = {};
    ajax('/Api/getVerify',
        data,
        function (res) {
            //   console.log("data:image/svg+xml;utf8,"+res.data);
            //  $("#img2").attr('src',"data:image/svg+xml;utf8,"+res.data);
            $("#img").empty();
            $("#img").append(res.data);
            //JSON.stringify
            loadingCode = false;
        },
        function (err) {

        })
}

function ajax(url, data, success, error) {
     var host =   window.location.origin;
    // var host = 'http://106.12.192.124:9000';
    //var host = 'http://127.0.0.1:7001';
    $.ajax({
        url: host + url,
        cache: false,
        type: "POST",
        contentType: 'application/json',
        data: JSON.stringify(data),
        beforeSend: function (xhr) {
            if (!fcode) {
                alert("\u53c2\u6570\u9519\u8bef"); //参数错误
            } else {
                xhr.setRequestHeader("token", fcode);
            }
        },
        success: success,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.responseJSON);
            // console.log(textStatus);
            error(XMLHttpRequest.responseJSON);

        }
    });
}

$("#img").on("click", function () {
    getVeriCode();
})


function testFF() {
    var object = this;
    this.isDebug = function () {
        var timeLimit = 50;
        var startTime = new Date();
        debugger; //只要一进入调试模式就会中断到此
        var endTime = new Date();
        if (endTime - startTime > timeLimit) { //通过中断时间来判断进入了调试模式,正常情况时间间隔在50毫秒以内
            $('form').remove(); //把要保护的内容先移除

            //alert('\u68c0\u6d4b\u5230\u975e\u6cd5\u8c03\u8bd5\uff0c\u5373\u5c06\u5237\u65b0\u754c\u9762\uff01');//提示: 检测到非法调试，即将刷新界面！
            window.location.reload(); //重新加载页面
        }
    };
    this.consoleListener = function () {
        setInterval(object.isDebug, 500) //500毫秒就会循环执行一次isDebug,
    }
}

;(function(){
    var url = localStorage.getItem('app');
    if(url){
        $(".download-BT,.first-download").attr("href",url)
    }         
})()
